<?php

namespace Beerus\Modules\DesignerAuth\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\service\admin\SaleBind;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\MessageBag;

class SaleBindController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		Admin::script('$(".pjax-container").attr("id","pjax-container");');
	}

	// 认领客户
	public function bind($uid)
	{
		$admin_id = Admin::user()->id;
		$admin_name = Admin::user()->username;

		$res = SaleBind::bind($uid, $admin_id, $admin_name);
		if ($res == SaleBind::STATUS_SUCCESS) {
			$success = new MessageBag([
				'title' => '认领成功'
			]);
			return back()->with(compact('success'));
		} else if ($res == SaleBind::STATUS_REPEAT) {
			$error = new MessageBag([
				'title' => '已经认领, 不需要重复操作'
			]);
			return back()->with(compact('error'));
		} else if ($res == SaleBind::STATUS_FAIL_BEBIND) {
			$error = new MessageBag([
				'title' => '认领失败，已被别人认领'
			]);
			return back()->with(compact('error'));
		} else if ($res == SaleBind::STATUS_FAIL_LIMIT) {
			$limit = SaleBind::BIND_LIMIT;
			$error = new MessageBag([
				'title' => '认领失败，超过认领上限:'. $limit
			]);
			return back()->with(compact('error'));
		} else {
			$error = new MessageBag([
				'title' => '认领失败'
			]);
			return back()->with(compact('error'));
		}
	}

	// 取消认领客户
	public function unbind($uid)
	{
		$admin_id = Admin::user()->id;
		$admin_name = Admin::user()->username;

		$res = SaleBind::unbind($uid, $admin_id, $admin_name);
		if ($res == SaleBind::STATUS_SUCCESS) {
			$success = new MessageBag([
				'title' => '取消认领成功'
			]);
			return back()->with(compact('success'));
		} else if ($res == SaleBind::STATUS_REPEAT) {
			$error = new MessageBag([
				'title' => '已经取消认领，不需要重复操作'
			]);
			return back()->with(compact('error'));
		} else if ($res == SaleBind::STATUS_FAIL_BEBIND) {
			$error = new MessageBag([
				'title' => '取消认领失败，只有本人才能取消认领'
			]);
			return back()->with(compact('error'));
		} else {
			$error = new MessageBag([
				'title' => '取消认领失败'
			]);
			return back()->with(compact('error'));
		}
	}
}
